#include "stdafx.h"
#include "Tank.h"
#include <fstream>
#include <stdio.h>

Tank::Tank()
{
}


Tank::~Tank()
{
}

int Tank::load(char* data_file)
{
	FILE* file = fopen(data_file, "r");
	m_sName = new char[100];
	m_sTexturePath = new char[100];
	int test;
	if (!file)
	{
		printf("File TankInfo not found!");
		return -1;
	}

	float hp, power, speed, sspeed, range;
	fscanf(file,
		"Name: %s\nTexture Path: %s\nHP: %f\nPower: %f\nSpeed: %f\nShooting Speed: %f\nRange: %f\nArmor: %f",
		m_sName, m_sTexturePath, &hp, &power, &speed, &sspeed, &range, &m_fArmor);
	m_iHP = (hp / 100.0f) * MAX_HP;
	m_iPower = (power / 100.0f) * MAX_POWER;
	m_iSpeed = (speed / 100.0f) * MAX_SPEED;
	m_iShootingSpeed = (sspeed / 100.0f) * MAX_SHOOTING_SPEED;
	m_iRange = (range / 100.0f) * MAX_RANGE;
	return 0;
}

void Tank::print()
{
	printf(
		"Name: %s\nTexture Path: %s\nHP: %i\nPower: %i\nSpeed: %i\nShooting Speed: %i\nRange: %i\nArmor: %f",
		m_sName, m_sTexturePath, m_iHP, m_iPower, m_iSpeed, m_iShootingSpeed, m_iRange, m_fArmor);
}

int Tank::save(char* data_file)
{
	FILE* file = fopen(data_file, "w");
	int test;
	if (!file)
	{
		printf("File TankInfo read-only!");
		return -1;
	}

	int hp, power, speed, sspeed, range;
	hp = 100.0f * float(m_iHP) / MAX_HP;
	power = 100.0f * float(m_iPower) / MAX_POWER;
	speed = 100.0f * float(m_iSpeed) / MAX_SPEED;
	sspeed = 100.0f * float(m_iShootingSpeed) / MAX_SHOOTING_SPEED;
	range = 100.0f * float(m_iRange) / MAX_RANGE;
	fprintf(file,
		"Name: %s\nTexture Path: %s\nHP: %i\nPower: %i\nSpeed: %i\nShooting Speed: %i\nRange: %i\nArmor: %f",
		m_sName, m_sTexturePath, hp, power, speed, sspeed, range, m_fArmor);
	return 0;
}